﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Synergy
{
    public partial class Formcxscrkqk : Form
    {
        public Formcxscrkqk()
        {
            InitializeComponent();
        }

        private void button刷新_Click(object sender, EventArgs e)
        {
           
                List金额();
           
        }

        internal static decimal ToNum(string p)
        {
            try
            {
                return decimal.Parse(p);
            }
            catch
            {
                return 0;
            }
        }

        private void List金额()
        {

            string sql;
            ///最大顺序号的报工单，未入库的
            sql = @" select '' hanghao, t.cMPoCode 生产加工单,t.hang hang,t.物料编号,t.物料名称,sum(t.加工单数量) 加工单数量, sum(t.入库数量) 入库数量, sum(t.报工数量) 报工数量 ,'' 是否关闭 from  ( 
                
                 select cMPoCode cMPoCode,imoseq hang,rdrecords10.cinvcode 物料编号,Inventory.cinvname 物料名称,sum(iquantity) 入库数量,0 加工单数量,0 报工数量
                 from rdrecords10 join rdrecord10 on 
                rdrecord10.id=rdrecords10.id join Inventory on Inventory.cinvcode=rdrecords10.cinvcode 
                group by cMPoCode,imoseq,rdrecords10.cinvcode ,Inventory.cinvname              
               
                union all
                
                select mocode cMPoCode,b.sortseq hang ,b.invcode 物料编号,Inventory.cinvname 物料名称,0 入库数量,qty 加工单数量,0 报工数量 from mom_order a join mom_orderdetail b on  a.moid=b.moid 
                join Inventory on Inventory.cinvcode=b.invcode 
               union all 
               
               select distinct 生产订单号 cMPoCode,生产订单行号 hang,物料编号,Inventory.cinvname 物料名称,0 入库数量,0 加工单数量,sum(报工数量) 报工数量  
               from Z_baogong join Inventory on Inventory.cinvcode=Z_baogong.物料编号   where  是否最大顺序='是最大顺序号'   
               group by  生产订单号,生产订单行号,物料编号,Inventory.cinvname  
                ) t where t.cMPoCode=t.cMPoCode     
               ";
          
            if (this.textBox1.Text != "")
            {
                sql += " and t.cMPoCode like '%" + this.textBox1.Text + "%'";
            }

            sql += "   group by t.cMPoCode,t.hang,t.物料编号,t.物料名称    ";
            DataTable tb = pub.U8GetTable(sql);
            this.dataGridView1.AutoGenerateColumns = false;
           
            this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            if (tb != null)
            {
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    tb.Rows[i]["hanghao"] = (i + 1).ToString();
                    string 生产订单号 = tb.Rows[i]["生产加工单"].ToString();
                    string 生产订单行号 = tb.Rows[i]["hang"].ToString();
                    if (Is生产订单关闭(生产订单号, 生产订单行号))
                    {
                        tb.Rows[i]["是否关闭"] = "已关闭";
                    }
                    else
                    {
                        tb.Rows[i]["是否关闭"] = "未关闭";
                    }
                }
            }         

            this.dataGridView1.DataSource = tb;
            this.dataGridView1.Refresh();       
        }

        private bool Is生产订单关闭(string 生产订单号, string 生产订单行号)
        {
            string sql;
            sql = "select isnull(关闭人员,'') from View_生产订单明细 where 生产订单号='" + 生产订单号 + "' and 行号='" + 生产订单行号 + "'";
            DataTable tb = pub.U8GetTable(sql);
            if (tb.Rows.Count > 0)
            {
                if (tb.Rows[0][0].ToString() == "")
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
                //string fileName = "";
                //string saveFileName = "";
                //SaveFileDialog saveDialog = new SaveFileDialog();
                //saveDialog.DefaultExt = "xls";
                //saveDialog.Filter = "Excel文件|*.xls";
                //saveDialog.FileName = fileName;
                //saveDialog.ShowDialog();
                //saveFileName = saveDialog.FileName;
                //if (saveFileName.IndexOf(":") < 0) return; //被点了取消
                //Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
                //if (xlApp == null)
                //{
                //    MessageBox.Show("无法创建Excel对象，您的电脑可能未安装Excel");
                //    return;
                //}
                //Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
                //Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                //Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 
                ////写入标题             
                //for (int i = 0; i < dataGridView1.ColumnCount; i++)
                //{ worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText; }
                ////写入数值
                //for (int r = 0; r < dataGridView1.Rows.Count; r++)
                //{
                //    for (int i = 0; i < dataGridView1.ColumnCount; i++)
                //    {
                //        worksheet.Cells[r + 2, i + 1] = dataGridView1.Rows[r].Cells[i].Value;
                //    }
                //    System.Windows.Forms.Application.DoEvents();
                //}
                //worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
                //MessageBox.Show(fileName + "资料保存成功", "提示", MessageBoxButtons.OK);
                //if (saveFileName != "")
                //{
                //    try
                //    {
                //        workbook.Saved = true;
                //        workbook.SaveCopyAs(saveFileName);  //fileSaved = true;                 
                //    }
                //    catch (Exception ex)
                //    {//fileSaved = false;                      
                //        MessageBox.Show("导出文件时出错,文件可能正被打开！\n" + ex.Message);
                //    }
                //}
                //xlApp.Quit();
                //GC.Collect();//强行销毁           }
            }
            
        }
    }

